<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
        $this->display('login');
    }

    public function login(){
        if(IS_POST){
            //dump(I('post.'));exit;
            $code = I('post.code');
            $name = I('post.name');
            $pass = md5(I('post.password'));
            if($this->check_verify($code ,$id='')){
				
                $users = M("admin_user a");
                $user = $users->where("password='%s' AND user_name='%s'",array($pass,$name))->find();
                if($user){
                    session('admin_user' ,$user);
                    cookie('admin_name' , $user);
                    $this->success('登录成功',U('index/index'));
                }else{
                    $this->error('登录失败');
                }
            }else{
                $this->error('验证码错误！');
            }
        }
    }
	
	/* 后台注册 */
	public function register(){
		
	}
	
	
    public function log_out(){
		unset($_SESSION['admin_user']);
		if(empty($_SESSION['admin_user'])){
			$this->success('退出成功',U('login/index'));
		}
    }

    public function code(){
        $Verify = new \Think\Verify();
        $Verify->imageW = 250;
        $Verify->fontSize = 30;
        $Verify->imageH = 60;
        $Verify->length = 4;
        $Verify->useCurve = false;
        $Verify->entry();
    }

    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }


}